package com.tianhai.db.parser.ast;

import java.util.List;

/**
 * CREATE TABLE语句AST节点
 */
public class CreateTableStatement extends Statement {
    
    private final String tableName;
    private final List<ColumnDefinition> columns;
    
    public CreateTableStatement(String tableName, List<ColumnDefinition> columns) {
        this.tableName = tableName;
        this.columns = columns;
    }
    
    @Override
    public StatementType getType() {
        return StatementType.CREATE_TABLE;
    }
    
    public String getTableName() {
        return tableName;
    }
    
    public List<ColumnDefinition> getColumns() {
        return columns;
    }
    
    @Override
    public String toString() {
        return "CreateTableStatement{" +
                "tableName='" + tableName + '\'' +
                ", columns=" + columns +
                '}';
    }
    
    /**
     * 列定义
     */
    public static class ColumnDefinition {
        private final String columnName;
        private final DataType dataType;
        private final Integer length; // 用于VARCHAR(n)
        
        public ColumnDefinition(String columnName, DataType dataType) {
            this(columnName, dataType, null);
        }
        
        public ColumnDefinition(String columnName, DataType dataType, Integer length) {
            this.columnName = columnName;
            this.dataType = dataType;
            this.length = length;
        }
        
        public String getColumnName() {
            return columnName;
        }
        
        public DataType getDataType() {
            return dataType;
        }
        
        public Integer getLength() {
            return length;
        }
        
        @Override
        public String toString() {
            if (length != null) {
                return columnName + " " + dataType + "(" + length + ")";
            }
            return columnName + " " + dataType;
        }
    }
    
    /**
     * 数据类型枚举
     */
    public enum DataType {
        INT,
        VARCHAR,
        TEXT,
        FLOAT,
        DOUBLE,
        BOOLEAN
    }
}
